package org.example.initializer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.dao.entity.Users;
import org.example.dao.mapper.UserMapper;
import org.example.feign.PermissionServiceFeignClient;
import org.example.mq.LogProduct;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Objects;

//初始化超管 没明白什么意思 当时写了一个方法手动生成超管
// 最后询问AI 可以利用SpringBoot的CommandLineRunner 来实现系统启动时就创建超管用户
@Component
@Slf4j
public class SuperAdminInitializer implements CommandLineRunner {

    @Resource
    private UserMapper userMapper;

    @Resource
    private PermissionServiceFeignClient permissionServiceFeignClient;

    @Resource
    private LogProduct logProduct;


    @Override
    public void run(String... args) throws Exception {
        //检查时候已经存在超级管理员
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("username", "cly");
        Users exist = userMapper.selectOne(qw);
        if(Objects.isNull(exist)) {
            //不存在则创建
            log.info("创建超级管理员");
            Users user = Users.builder()
                    .userName("cly")
                    .password("cly")
                    .email("2120473379@qq.com")
                    .phone("18338240514")
                    .build();
            userMapper.insert(user);

            log.info("绑定默认角色");
            permissionServiceFeignClient.bindSuperAdminRole(user.getUserId());
            log.info("已初始化超级管理员账号: {} 密码: {}","cly","cly");

        } else {
            log.info("超级管理员已存在，跳过初始化");
        }
    }
}
